# -*- coding: utf-8 -*-
"""
# @created on : 9月 2021/9/15 00:55
# @author     : kevin
# @file       : api_request.py
# @software   : PyCharm
# @project    : auto_interface_test


@license: (C) Copyright 2019-2025, Corporation Limited.
@desc:
"""
import requests
import json
from .dubbo_api import InvokeDubboApi
from common.logger import Logger


class RunMethod:
    def __init__(self):
        self.log = Logger()

    def post_main(self, url, data, header=None):
        res = None
        try:
            if header:
                res = requests.post(url=url, data=data, headers=header, timeout=(10, 20), verify=False)
            else:
                res = requests.post(url=url, data=data, timeout=(10, 20), verify=False)
        except (requests.exceptions.ConnectionError, requests.exceptions.ConnectTimeout) as e:
            self.log.logger('error', f'requests error is {e}')
        return res

    def get_main(self, url, data=None, header=None):
        res = None
        try:
            if header:
                res = requests.get(url=url, params=data, headers=header, timeout=(10, 20), verify=False)
            else:
                res = requests.get(url=url, params=data, timeout=(10, 20), verify=False)
        except (requests.exceptions.ConnectionError, requests.exceptions.ConnectTimeout) as e:
            self.log.logger('error', f'requests error is {e}')
        return res

    def dubbo_main(self, url, data=None):
        res = None
        dubbo = InvokeDubboApi(url)
        if isinstance(data, list):
            if len(data) > 1:
                res = dubbo.invoke_dubbo_apis(data)
            else:
                res = dubbo.invoke_dubbo_api(data)
        else:
            self.log('error', f'data不是元祖类型，当前类型为：{type(data)}，请处理后重新测试')
        return res

    def method_main(self, method, url, data=None, header=None):
        res = None
        print(f'{url}')
        if method.upper() == 'POST':
            res = self.post_main(url=url, data=data, header=header)
        elif method.upper() == 'GET':
            res = self.get_main(url=url, data=data, header=header)
        elif method.upper() == 'DUBBO':
            res = self.dubbo_main(url=url, data=data)
        else:
            pass
        return res
